<?php
use TbfRuntime\SingleThreadExitException;
use TbfRuntime\SingleThreadTestRuntime;
use TbfProtocol\SingleThreadTestProtocol;
class TbfTest{
    public static $origin_SERVER = array();
    /**
     * 把所有内部全局状态设置到一开始的状态
     * @return string
     */
    public static function reset(){
        $_GET = array();
        $_POST = array();
        $_REQUEST = array();
        $_COOKIE = array();
        $_SESSION = array();
        $_SERVER = self::$origin_SERVER;
        $_FILES = array();
        TbfSession::clear();
        TbfProtocol::$isMockView = false; //默认api调用是正常的
        self::$lastContent = '';
        SingleThreadTestProtocol::$lastResponse = null;
    }
    /**
     * 调用api,$_GET等请在调用之前手动写入值
     * @return void
     */
    public static function callApi(){
        TbfProtocol::$isMockView = false;
        ob_start();
        \TbfControllerRunner::run();
        self::$lastContent = ob_get_clean();
    }

    //mock掉所有输出接口,然后调用api
    public static function callApiWithMock(){
        TbfProtocol::$isMockView = true;
        ob_start();
        \TbfControllerRunner::run();
        self::$lastContent = ob_get_clean();
        return self::getLastResponse();
    }
    /**
     * 调用api,$_GET等请在调用之前手动写入值
     * @return void
     */
    public static function callApiEcho(){
        \TbfControllerRunner::run();
    }

    static function requestAsPost(){
        $_SERVER['REQUEST_METHOD'] = 'POST';
    }

    /** @return string */
    public static function getLastContent(){
        return self::$lastContent;
    }
    /** @return object */
    public static function getLastJson(){
        return TbfEncoding::JsonDecode(self::$lastContent);
    }

    public static function getLastResponse(){
        return SingleThreadTestProtocol::$lastResponse;
    }

    /** @var string  */
    private static $lastContent = '';
}